/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.base;

import dex.misc.Mirror;


/**
 * A place of origin for a parse tree node.
 */
final public class Place {
	
	
	/**
	 * The filename where the node originated.
	 */
	private String fileName;


	/**
	 * The line number where the node originated.
	 */
	private int lineNumber;
	
	
	/**
	 * The character number where the node originated.
	 * This is the character number of the line, not of the
	 * entire file.
	 */
	private int characterNumber;
	
	
	/**
	 * Constructs a new place of origin.
	 * 
	 * @param fileName   the name of the file where the node originated
	 * @param lineNumber  the line number in that file where the node originated
	 * @param characterNumber  the character number in that line where the node originated
	 */
	public Place(String fileName, int lineNumber, int characterNumber) {
		super();
		this.fileName = fileName;
		this.lineNumber = lineNumber;
		this.characterNumber = characterNumber;
	}


	/**
	 * The character number where the node originated.
	 * This is the character number of the line, not of the
	 * entire file.
	 * 
	 * <p>A return value of zero indicates that the origin is
	 * unknown.  This can happen if the node were constructed
	 * by some macro and not by the parser.
	 * 
	 * @return  the character number, or 0 if unknown
	 */
	public int getCharacterNumber() {
		return characterNumber;
	}


	/**
	 * The name of the file where the node originated.
	 * This can be null if the origin is unknown.  That
	 * can happen if the node were constructed by some
	 * macro and not by the parser.
	 * 
	 * @return  the name of the file, or null if unknown
	 */
	public String getFileName() {
		return fileName;
	}


	/**
	 * The line number where the node originated.
	 * A return value of zero indicates that the origin is
	 * unknown.  This can happen if the node were constructed
	 * by some macro and not by the parser.
	 * 
	 * @return  the line number where the node originated, or 0 if unknown
	 */
	public int getLineNumber() {
		return lineNumber;
	}

	
	/**
	 * Returns a friendly string representation of this place.
	 */
	public String toString() {
		String name = (fileName == null) ? "Unknown" : fileName;
		return name + "[" + lineNumber + "," + characterNumber + "]";
	}

	
	/**
	 * Returns true if the given object is the same as this place.
	 * 
	 * @param o  the object to test
	 */
	public boolean equals(Object o) {
		return Mirror.equals(this, o);
	}

}
